package com.todo.content.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.enums.EnableTypeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.content.application.backend.service.ContentTopicBackendService;
import com.todo.content.application.convert.ContentConvert;
import com.todo.content.application.entity.bo.ContentTopicBO;
import com.todo.content.application.entity.condition.ContentTopicConditionBO;
import com.todo.content.application.service.ContentTopicService;
import com.todo.content.iface.backend.request.*;
import com.todo.content.iface.backend.response.ContentTopicDetailByIdResp;
import com.todo.content.iface.backend.response.QueryContentTopicByConditionResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (ContentTopic)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:46
 */
@Slf4j
@Service("contentTopicBackendService")
public class ContentTopicBackendServiceImpl implements ContentTopicBackendService {

    @Resource
    private ContentTopicService contentTopicService;

    @Override
    public BizResponse<Boolean> insertContentTopic(InsertContentTopicReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentTopicBO contentTopicBO = ContentConvert.convertToContentTopicBO(req);
        Boolean insertResult = contentTopicService.insertContentTopic(contentTopicBO);
        log.info("ContentTopicBackendService#insertContentTopic 新增话题信息完成 入参：【{}】出参：【{}】", JSONObject.toJSONString(req), insertResult);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> updateContentTopicById(UpdateContentTopicByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentTopicBO contentTopicBO = ContentConvert.convertToContentTopicBO(req);
        Boolean updateResult = contentTopicService.updateContentTopic(contentTopicBO);
        log.info("ContentTopicBackendService#updateContentTopicById 更新话题信息完成 入参：【{}】出参：【{}】", JSONObject.toJSONString(req), updateResult);
        return BizResponse.success(updateResult);
    }

    @Override
    public BizResponse<Boolean> removeContentTopicById(RemoveContentTopicByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        //todo 是否创建用户，或者admin用户
        Boolean removeResult = contentTopicService.removeContentTopicById(req.getTopicId());
        log.info("ContentTopicBackendService#removeContentTopicById 删除话题信息完成 入参：【{}】出参：【{}】", JSONObject.toJSONString(req), removeResult);
        return BizResponse.success(removeResult);
    }

    @Override
    public BizResponse<Boolean> enableContentTopicById(EnableContentTopicByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        Boolean enableResult = contentTopicService.enableContentTopicById(req.getId(), EnableTypeEnum.getByType(req.getEnable()));
        log.info("ContentTopicBackendService#enableContentTopicById 启用禁用话题信息完成 入参：【{}】出参：【{}】", JSONObject.toJSONString(req), enableResult);
        return BizResponse.success(enableResult);
    }

    @Override
    public BizResponse<ContentTopicDetailByIdResp> queryContentTopicById(QueryContentTopicByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentTopicBO contentTopicBO = contentTopicService.queryContentTopicById(req.getTopicId());
        log.info("ContentTopicBackendService#queryContentTopicById 查询话题信息完成 入参：【{}】出参：【{}】", JSONObject.toJSONString(req), JSONObject.toJSONString(contentTopicBO));
        return BizResponse.success(ContentConvert.convertToContentTopicDetailByIdResp(contentTopicBO));
    }

    @Override
    public BizPageResponse<QueryContentTopicByConditionResp> queryContentTopicByCondition(QueryContentTopicByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentTopicConditionBO conditionBO = ContentConvert.convertToContentTopicConditionBO(req);
        PageResult<ContentTopicBO> pageResult = contentTopicService.queryContentTopicListByCondition(conditionBO);
        return BizPageResponse.success(ContentConvert.convertToPageQueryContentTopicByConditionResp(pageResult));
    }
}
